
function TreeNode(val, left, right) {
    this.val = (val===undefined ? 0 : val)
    this.left = (left===undefined ? null : left)
    this.right = (right===undefined ? null : right)
}

/**
 * @param {number[]} preorder
 * @param {number[]} inorder
 * @return {TreeNode}
 */
var buildTree = function(preorder, inorder) {
  if(preorder.length === 0)
    return null;
  let root = new TreeNode(preorder[0]);
  let mid = inorder.indexOf(preorder[0]);
  if(mid === -1)
    return null;
  root.left = buildTree(preorder.slice(1, mid + 1), inorder.slice(0, mid));
  root.right = buildTree(preorder.slice(mid + 1, preorder.length), inorder.slice(mid + 1, inorder.length));
  return root;
};
let preorder = [3,9,20,15,7], inorder = [9,3,15,20,7];
console.log(buildTree(preorder, inorder));
preorder = [-1], inorder = [-1];
console.log(buildTree(preorder, inorder));
// let arr = [1, 2, 3, 4, 5];
// console.log(arr.slice(0, 3));
// console.log(arr.slice(2, 4));
// console.log(arr.slice(0, 5));
// let preorder = [3,9,20,15,7], inorder = [9,3,15,20,7], mid = 1;
// console.log(preorder.slice(1, mid + 1), preorder.slice(mid + 1, preorder.length));
// console.log(inorder.slice(0, mid), inorder.slice(mid + 1, inorder.length));
